//
//  TypeTransformVC.swift
//  swiftDemo
//
//  Created by mac on 2017/3/28.
//  Copyright © 2017年 毕小强. All rights reserved.
//

import UIKit

class TypeTransformVC: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        let library = [
            Movie(name: "青歌行", direcor: "李白"),
            Song(name: "<逆流成河>", artist: "金"),
            Movie(name: "十面埋伏", direcor: "冯小刚"),
            Song(name: "霸王别姬", artist: "李玉刚"),
            Song(name: "童话镇", artist: "陈一发儿"),
            MediaItem(name: "流媒体")
        ]
        
        var movieCount = 0
        var songCount = 0
        
        for item in library {
            if item is Movie {
                movieCount += 1
            }else if item is Song{
                songCount += 1
            }

        }
        print("电影：\(movieCount)  歌:\(songCount)")

        for item in library {
            if let movie = item as? Movie {
                print("电影:\(movie.name) 导演:\(movie.direcTor)")
            }else if let song = item as? Song{
                print("歌名:\(song.name)  歌手:\(song.artist)")
            }
        }
        
        //Any 和 AnyObject 的类型转换
        var things = [Any]()
        
        things.append(0)
        things.append(0.0)
        things.append(42)
        things.append(3.14159)
        things.append("hello")
        things.append((3.0, 5.0))
        things.append(Movie(name: "Ghostbusters", direcor: "Ivan Reitman"))
        things.append({ (name: String) -> String in "Hello, \(name)" })
        
        for thing in things {
            switch thing {
            case let someInt as Int:
                print("\(someInt) as an Int")
            case let someFloat as Double:
                print("\(someFloat) as an Double")
            case let somestring as String:
                print("\(somestring) as an String")
            case let movie as Movie:
                print("电影名:\(movie.name) 导演:\(movie.direcTor)")
            case let (x,y) as (Double,Double):
                 print("an (x, y) point at \(x), \(y)")
            case let stringConverter as (String) -> String:
                print(stringConverter("Jack"))
            default:
                print("something else")
            }
        }
        
        let optionalNumber:Int? = 3
        things.append(optionalNumber ?? 0)
        things.append(optionalNumber as Any)
        
        // Do any additional setup after loading the view.
    }

    class MediaItem {
        var name:String
        init(name:String) {
            self.name = name
        }
    }
    
    class Movie: MediaItem {
        var direcTor:String
        init(name:String,direcor:String) {
            self.direcTor = direcor
            super.init(name: name)
        }
    }
    
    class Song: MediaItem {
        var artist:String
        init(name:String,artist:String) {
            self.artist = artist
            super.init(name: name)
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
